package ub.springframework.hardware.hik.util;

import ub.springframework.hardware.hik.entity.HikPtzControlOther;
import ub.springframework.hardware.hik.entity.HikPtzPresetOther;
import ub.springframework.hardware.hik.enums.GetLastErrorEnum;
import ub.springframework.hardware.hik.hik.HikSdk;

/**
 * hik 操作工具
 *
 * @author 祥霸
 * @version 1.0.0
 */
public class HikUtil {

    /**
     * 云台控制
     *
     * @param in 参数
     *
     * @return 状态码 详见 GetLastError.pdf
     */
    public int hikPtzControlOther(HikPtzControlOther in) {
        int flag = -1;
        HikSdk hik = new HikSdk(in.getIpc().getIp(), (short) in.getIpc().getPort(), in.getIpc().getUsername(), in.getIpc().getPassword());
        try {
            // 初始化SDK
            hik.initSdk();

            // 注册设备
            flag = hik.loginSdk();
            if (GetLastErrorEnum.NET_DVR_NOERROR.getValue() != flag) {
                return flag;
            }

            // 操作设备
            flag = hik.hikPtzControlWithSpeed(in.getIpc().getChannel(), in.getDwPtzCommand().getValue(), in.getDwStop(), in.getDwSpeed());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 注销设备
            hik.logoutSdk();

            // 释放SDK资源
            hik.cleanUp();
        }

        return flag;
    }

    /**
     * 云台预置点操作
     *
     * @param in 参数
     * @return 状态码 详见 GetLastError.pdf
     */
    public int ptzPresetOther(HikPtzPresetOther in) {
        HikSdk hik = new HikSdk(in.getIpc().getIp(), (short) in.getIpc().getPort(), in.getIpc().getUsername(), in.getIpc().getPassword());

        int flag = -1;
        try {
            // 初始化SDK
            hik.initSdk();

            // 注册设备
            flag = hik.loginSdk();
            if (GetLastErrorEnum.NET_DVR_NOERROR.getValue() != flag) {
                return flag;
            }

            // 云台预置点操作
            flag = hik.ptzPresetOther(in.getIpc().getChannel(), in.getDwPtzPresetCmd().getValue(), in.getDwPresetIndex());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 注销设备
            hik.logoutSdk();
            // 释放SDK资源
            hik.cleanUp();
        }
        return flag;
    }
}
